<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>WebSocket Test</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .container {
            border: 1px solid #ccc;
            padding: 20px;
            border-radius: 5px;
        }
        .status {
            margin-bottom: 10px;
            padding: 10px;
            border-radius: 3px;
        }
        .connected {
            background-color: #d4edda;
            color: #155724;
        }
        .disconnected {
            background-color: #f8d7da;
            color: #721c24;
        }
        .message-area {
            margin-top: 20px;
        }
        #messages {
            height: 300px;
            overflow-y: auto;
            border: 1px solid #ccc;
            padding: 10px;
            margin-bottom: 10px;
        }
        .input-area {
            display: flex;
        }
        #messageInput {
            flex: 1;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 3px;
        }
        button {
            padding: 10px 15px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 3px;
            cursor: pointer;
            margin-left: 5px;
        }
        button:hover {
            background-color: #0069d9;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>WebSocket Test</h1>
        <div class="status disconnected" id="status">
            Disconnected
        </div>
        
        <div class="controls">
            <button id="connectBtn">Connect</button>
            <button id="disconnectBtn" disabled>Disconnect</button>
        </div>
        
        <div class="message-area">
            <div id="messages"></div>
            <div class="input-area">
                <input type="text" id="messageInput" placeholder="Enter message...">
                <button id="sendBtn" disabled>Send</button>
            </div>
        </div>
    </div>

    <script>
        const statusElement = document.getElementById('status');
        const connectBtn = document.getElementById('connectBtn');
        const disconnectBtn = document.getElementById('disconnectBtn');
        const sendBtn = document.getElementById('sendBtn');
        const messageInput = document.getElementById('messageInput');
        const messagesElement = document.getElementById('messages');

        let socket = null;
        // Generate a random peer ID for testing
        const peerId = 'test-' + Math.random().toString(36).substring(2, 10);
        const wsUrl = `ws://192.168.50.177:3000/peerjs-server/peerjs?key=default-api-key&token=default-api-token&id=${peerId}`;

        function updateStatus(isConnected) {
            if (isConnected) {
                statusElement.className = 'status connected';
                statusElement.textContent = 'Connected';
                connectBtn.disabled = true;
                disconnectBtn.disabled = false;
                sendBtn.disabled = false;
            } else {
                statusElement.className = 'status disconnected';
                statusElement.textContent = 'Disconnected';
                connectBtn.disabled = false;
                disconnectBtn.disabled = true;
                sendBtn.disabled = true;
            }
        }

        function appendMessage(message, isOutgoing = false) {
            const messageElement = document.createElement('div');
            messageElement.className = isOutgoing ? 'outgoing' : 'incoming';
            messageElement.textContent = isOutgoing ? `You: ${message}` : `Server: ${message}`;
            messagesElement.appendChild(messageElement);
            messagesElement.scrollTop = messagesElement.scrollHeight;
        }

        connectBtn.addEventListener('click', () => {
            if (socket && socket.readyState === WebSocket.OPEN) {
                appendMessage('Already connected');
                return;
            }

            socket = new WebSocket(wsUrl);

            socket.onopen = () => {
                updateStatus(true);
                appendMessage('Connected to server');
            };

            socket.onmessage = (event) => {
                appendMessage(event.data);
            };

            socket.onclose = (event) => {
                updateStatus(false);
                appendMessage(`Disconnected from server. Code: ${event.code}, Reason: ${event.reason}`);
            };

            socket.onerror = (error) => {
                appendMessage(`Error: ${error.message}`);
                updateStatus(false);
            };
        });

        disconnectBtn.addEventListener('click', () => {
            if (socket) {
                socket.close();
                socket = null;
            }
        });

        sendBtn.addEventListener('click', () => {
            const message = messageInput.value.trim();
            if (message && socket && socket.readyState === WebSocket.OPEN) {
                socket.send(message);
                appendMessage(message, true);
                messageInput.value = '';
            }
        });

        messageInput.addEventListener('keypress', (event) => {
            if (event.key === 'Enter') {
                sendBtn.click();
            }
        });
    </script>
</body>
</html>